package com.ks.controller;

import com.ks.base.BaseAction;
import com.ks.entity.Unit;
import com.ks.entity.User;
import com.ks.service.UnitService;
import com.ks.util.HtmlUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("dept")
public class DeptController extends BaseAction {


    @Autowired(required = false)
    private UnitService unitService;


    @RequestMapping("/dataList")
    public void datalist(HttpServletResponse response) throws Exception {
        List<Unit> unitList = unitService.queryList(null);
        HtmlUtil.writerJson(response, unitList);
    }

    @RequestMapping("unitindex")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("pages/dept/dept");
        return mv;
    }

    @RequestMapping("unitdata")
    public ModelAndView unitdata(HttpServletRequest request) throws Exception {
        String id = request.getParameter("id");
        ModelAndView mv = new ModelAndView();
        Unit unit = (Unit)unitService.queryById(id);
        if(unit != null) {
            mv.addObject("unit",unit);
        }
        mv.setViewName("pages/dept/dept-add");
        return mv;
    }

    @RequestMapping("/save")
    @ResponseBody
    public void save(Unit entity, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        HttpSession session = request.getSession(true);
        User user = (User) session.getAttribute("userInfo");
        boolean bool =  unitService.queryCode(entity.getCode());
        boolean boolidentifier =  unitService.queryIdentifier(entity.getIdentifier());
        if (entity.getId() == null || StringUtils.isBlank(entity.getId().toString())) {
            if (bool && boolidentifier) {
                unitService.add(entity);
                resultMap.put("status",1);
            } else if(!bool && boolidentifier) {
                resultMap.put("status",0);
                resultMap.put("Msg","代码重复");
            } else if(!boolidentifier && bool) {
                resultMap.put("status",0);
                resultMap.put("Msg","编号重复");
            } else {
                resultMap.put("status",0);
                resultMap.put("Msg","代码重复和编号都重复");
            }
        } else {
            Unit unit = (Unit) unitService.queryById(entity.getId());
            if(unit.getCode().equals(entity.getCode())&&unit.getIdentifier().equals(entity.getIdentifier())) {
                unitService.updateBySelective(entity);
                resultMap.put("status",1);
            } else {
                if(unit.getCode().equals(entity.getCode())) {
                    if (!boolidentifier) {
                        resultMap.put("status",0);
                        resultMap.put("Msg","编号重复");
                    } else {
                        unitService.updateBySelective(entity);
                        resultMap.put("status",1);
                    }
                }else if (unit.getIdentifier().equals(entity.getIdentifier())) {
                    if (!bool) {
                        resultMap.put("status",0);
                        resultMap.put("Msg","代码重复");
                    } else {
                        unitService.updateBySelective(entity);
                        resultMap.put("status",1);
                    }
                } else {

                    if (bool && boolidentifier) {
                        unitService.updateBySelective(entity);
                        resultMap.put("status",1);
                    } else if(!bool && boolidentifier) {
                        resultMap.put("status",0);
                        resultMap.put("Msg","代码重复");
                    } else if(!boolidentifier && bool) {
                        resultMap.put("status",0);
                        resultMap.put("Msg","编号重复");
                    } else {
                        resultMap.put("status",0);
                        resultMap.put("Msg","代码重复和编号都重复");
                    }
                }
            }

        }


        HtmlUtil.writerJson(response, resultMap);

    }


    @RequestMapping("/delpaper")
    public void del(Unit entity, HttpServletResponse response) throws Exception {

        unitService.del(entity);
        sendSuccessMessage(response, "删除成功");
    }

    @RequestMapping("/codeAjax")
    @ResponseBody
    public Boolean codeAjax(String code){
        return unitService.queryCode(code);
    }

    @RequestMapping("/identifierAjax")
    @ResponseBody
    public Boolean identifierAjax(String identifier){
        return unitService.queryIdentifier(identifier);
    }
}
